package com.usian.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.usian.reggie.common.R;
import com.usian.reggie.entity.Employee;
import com.usian.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 用户登录
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){

        R<Employee> result = employeeService.findByName(employee);

        //判断登录是否成功
        if(result.getCode()==1){
            session.setAttribute("user",result.getData());
        }
      return result;
    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        //退出把session清楚
        session.removeAttribute("user");
        return R.success("退出成功");
    }

    /**
     * 保存员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R save (@RequestBody Employee employee,HttpSession session){
        Employee employee1 = (Employee)session.getAttribute("user");

        //保存和修改
        if(employee.getId()==null){
            if(employee1!=null){
                employee.setCreateUser(employee1.getId());
                employee.setUpdateUser(employee1.getId());
            }
            employeeService.save(employee);
        }else{
            //修改
            employeeService.updateEmp(employee);
        }

        return  R.success("保存成功");
    }


    /**
     * 分页查询员工列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10")Integer pageSize,String name){

        PageInfo<Employee> employeePage = employeeService.findPage(page, pageSize, name);
        return R.success(employeePage);
    }

    /**
     * 员工启用、禁用
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody Employee employee,HttpSession session){

        Employee employee1 = (Employee)session.getAttribute("user");
        if(employee1!=null){
            employee.setUpdateUser(employee1.getId());
        }
        employeeService.updateEmp(employee);
        return R.success("修改成功");
    }

    /**
     * 编辑回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        Employee employee = employeeService.findById(id);
        return R.success(employee);
    }

}
